package controller

import (
	"github.com/mjiulee/go-sessions"
	"github.com/mjiulee/lego"
	"github.com/mojocn/base64Captcha"
	"github.com/valyala/fasthttp"
	. "saas_sys_base/modules/adminSaas/service"
	"strconv"
)

func init() {
	vc := new(AdminSaasLoginCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *AdminSaasLoginCtrl) CtrlInfo() string {
	return "AdminSaasLoginCtrl"
}

func (vc *AdminSaasLoginCtrl) RoutInit() {
	lego.GET("/adminSaas/login", vc.getLogin, lego.LEGO_SESSION_TYPE_ADMIN, false)
	lego.POST("/adminSaas/login", vc.postLogin, lego.LEGO_SESSION_TYPE_ADMIN, false) // 登录
	lego.GET("/adminSaas/logout", vc.doLogOut, lego.LEGO_SESSION_TYPE_ADMIN, true)   // 登出
	lego.GET("/adminSaas/captchar/reflash", vc.GetCaptchar, lego.LEGO_SESSION_TYPE_ADMIN, false)
}

/* ************************************************************************************ */
/* 类型定义 */
type AdminSaasLoginCtrl struct {
	service AdminSaasLoginService
}

/* 获取登陆页面
 * params:
 */
func (vc *AdminSaasLoginCtrl) getLogin(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	vid, imgdata := vc.service.GetCaptchar()

	data := make(map[string]interface{})
	data["vid"] = vid
	data["imgdata"] = imgdata

	ctxExt.HTML("system/view/SysLogin/login.html", data)
	return
}

/* 退出登陆
 * params:
 */
func (vc *AdminSaasLoginCtrl) doLogOut(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	sessions.DestroyFasthttp(ctx)
	//ctxExt.Redirect("/saas_sys_base/admin/login", 302)
	response := make(map[string]interface{})
	code := 0
	msg := "success"
	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
}

/* 登陆提交
 * params:
 */
func (vc *AdminSaasLoginCtrl) postLogin(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	// 表单校验
	type ReqParams struct {
		Account  string `vd:"len($)>=0"` // 账号
		Password string `vd:"len($)>0"`  // 密码
		Captcha  string `vd:"len($)>0"`  // 密码
		Vid      string `vd:"len($)>0"`  // 密码
	}
	req := ReqParams{
		Account:  string(ctxExt.Peek("account")),
		Password: string(ctxExt.Peek("password")),
		Captcha:  string(ctxExt.Peek("captcha")),
		Vid:      string(ctxExt.Peek("vid")),
	}

	for {
		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		verifyResult := base64Captcha.VerifyCaptcha(req.Vid, req.Captcha)
		if false == verifyResult {
			code = 100
			msg = "验证码错误"
			break
		}

		user, err := vc.service.CheckLoginUser(req.Account, req.Password)
		if err != nil {
			code = 100
			msg = err.Error()
			break
		}

		sess := sessions.StartFasthttp(ctx) // init the session

		id := strconv.FormatInt(user.Id, 10)
		// 设置session
		values := map[string]interface{}{
			"user_id": id,
			"Days":    "1",
			"Secret":  "dsads£2132215£%%Ssdsa",
		}
		for k, v := range values {
			sess.Set(k, v) // fill session, set each of the key-value pair
		}
		// 更新登陆时间
		go vc.service.SaveLoginLog(user, "", "")
		break
	}
	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
}

/**
刷新2维码
*/
func (vc *AdminSaasLoginCtrl) GetCaptchar(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	vid, imgdata := vc.service.GetCaptchar()
	data := make(map[string]interface{})
	data["captchaId"] = vid
	data["base64Png"] = imgdata

	response["code"] = code
	response["msg"] = msg
	response["data"] = data
	ctxExt.JSON(200, response)
	return
}
